package com.enfo.zjabank.controller;

import com.enfo.zjabank.service.CommentService;
import com.enfo.zjabank.tool.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhoum on 2016/8/4.
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @RequestMapping(value = "/toComment",method = RequestMethod.GET)
    public String toComment(){
//        return "/comment/comment";
//        response.sendRedirect(request.getContextPath()+"/comment/comment.jsp");
        return "forward:/comment/comment.jsp";
    }

    @RequestMapping(value="/list/{id}",method = RequestMethod.GET)
    public @ResponseBody
    List<Map<String, Object>> listComments(@PathVariable String id){
//        Map<String,String> map = new HashMap<>();
//        List<Map> list = new ArrayList<>();
//        map.put("user", "用户1");
//        map.put("context", "评论正文[em_1]");
//        map.put("time", "2016-01-01 00:00");
//        list.add(map);
//        map = new HashMap<>();
////        map.clear();
//        System.out.println(list);
//        map.put("user", "用户2");
//        map.put("context", "评论正文[em_2]");
//        map.put("time", "2016-01-01 00:00");
//        list.add(map);
        if(!StringUtil.isEmpty(id)){
            return commentService.listComments(id);
        }
        return null;
    }

    @RequestMapping(value = "/publish",method = RequestMethod.POST)
    public @ResponseBody List<Map> publish(HttpServletRequest request,HttpServletResponse response){
        String commentText = request.getParameter("commentText");
        String inputUser = request.getParameter("inputUser");
        String id = request.getParameter("id");
        if(!StringUtil.isEmpty(commentText)&&!StringUtil.isEmpty(inputUser)&&!StringUtil.isEmpty(id)){
            List<Map> list = new ArrayList<>();
//            Map<String,String> map = new HashMap<>();
//            map.put("user","用户3");
//            map.put("context",commentText);
//            map.put("time","2016-08-04 14:10");
//            list.add(map);
            list.add(commentService.publish(id,commentText,inputUser));
            return list;
        }
        return null;
    }
}
